package cn.iocoder.yudao.module.mqtt.controller.admin.subscribewater;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.subscribewater.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.subscribewater.SubscribeWaterDO;
import cn.iocoder.yudao.module.mqtt.service.subscribewater.SubscribeWaterService;

@Tag(name = "管理后台 - MQTT水监控")
@RestController
@RequestMapping("/mqtt/subscribe-water")
@Validated
public class SubscribeWaterController {

    @Resource
    private SubscribeWaterService subscribeWaterService;

    @PostMapping("/create")
    @Operation(summary = "创建MQTT水监控")
    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:create')")
    public CommonResult<Long> createSubscribeWater(@Valid @RequestBody SubscribeWaterSaveReqVO createReqVO) {
        return success(subscribeWaterService.createSubscribeWater(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MQTT水监控")
    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:update')")
    public CommonResult<Boolean> updateSubscribeWater(@Valid @RequestBody SubscribeWaterSaveReqVO updateReqVO) {
        subscribeWaterService.updateSubscribeWater(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MQTT水监控")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:delete')")
    public CommonResult<Boolean> deleteSubscribeWater(@RequestParam("id") Long id) {
        subscribeWaterService.deleteSubscribeWater(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除MQTT水监控")
                @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:delete')")
    public CommonResult<Boolean> deleteSubscribeWaterList(@RequestParam("ids") List<Long> ids) {
        subscribeWaterService.deleteSubscribeWaterListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MQTT水监控")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:query')")
    public CommonResult<SubscribeWaterRespVO> getSubscribeWater(@RequestParam("id") Long id) {
        SubscribeWaterDO subscribeWater = subscribeWaterService.getSubscribeWater(id);
        return success(BeanUtils.toBean(subscribeWater, SubscribeWaterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MQTT水监控分页")
//    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:query')")
//    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:query') || @ss.hasPermission('device:history')")
    public CommonResult<PageResult<SubscribeWaterRespVO>> getSubscribeWaterPage(@Valid SubscribeWaterPageReqVO pageReqVO) {
        PageResult<SubscribeWaterDO> pageResult = subscribeWaterService.getSubscribeWaterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SubscribeWaterRespVO.class));
    }

    @GetMapping("/getZxt")
    @Operation(summary = "获得MQTT水监控分页")
    public CommonResult<Map<String, Object>> getZxt(@RequestParam("deviceKey") String deviceKey) {
        Map<String, Object> map = subscribeWaterService.getZtx(deviceKey);
        return success(map);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出MQTT水监控 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:subscribe-water:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSubscribeWaterExcel(@Valid SubscribeWaterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubscribeWaterDO> list = subscribeWaterService.getSubscribeWaterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MQTT水监控.xls", "数据", SubscribeWaterRespVO.class,
                        BeanUtils.toBean(list, SubscribeWaterRespVO.class));
    }

}